<template>
  <div class="">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
      class="form-search"
    >
      <SearchTool
        :code="{
          factory: 'factory',
          workshop: 'workShopCode',
          line: 'lineCode',
          section: 'sectionCode',
        }"
        :titles="{
          factory: '工厂',
          workshop: '工段',
          line: '产线',
          section: '工艺段',
        }"
        v-model:factory="queryParams.factory"
        v-model:workshop="queryParams.workShopCode"
        v-model:line="queryParams.lineCode"
        v-model:section="queryParams.sectionCode"
      ></SearchTool>
      <el-form-item label="线边仓" prop="storehouseCode">
        <StoreSelect
          v-model="queryParams.storehouseCode"
          :line="queryParams.lineCode"
          :section="queryParams.sectionCode"
          placeholder="请选择线边仓"
        ></StoreSelect>
      </el-form-item>
      <el-form-item :label="titleName + '编号'" prop="matCode">
        <el-input
          v-model="queryParams.matCode"
          :placeholder="`请输入${titleName}编号`"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="titleName + '名称'" prop="matName">
        <el-input
          v-model="queryParams.matName"
          :placeholder="`请输入${titleName}名称`"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次号" prop="batchNo">
        <el-input
          v-model="queryParams.batchNo"
          placeholder="请输入批次号"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
        <el-button icon="refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="stockList"
      border
      maxHeight="500"
      show-overflow-tooltip
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        v-for="(item, index) in columns"
        :key="index"
        :label="item.label"
        :width="item.width"
        align="center"
        :prop="item.prop"
      >
        <template v-if="item.link" #default="scope">
          <el-link type="primary" @click="handleDetail(scope.row)">
            {{ scope.row[item.prop] }}
          </el-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <matDetail v-model:show="open" :type="name" :formData="formData" />
  </div>
</template>

<script>
import { listStock, getStock } from "@/api/warehouse/stock";
import {floatSub} from "@/utils/mathCount"
import matDetail from "./matDetail.vue";
export default {
  name: "StockTable",
  components: {
    matDetail,
  },
  props: {
    columns: {
      type: Array,
      default: () => [],
    },
    name: {
      type: String,
      default: "mat",
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实时库存表格数据
      stockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        factory: undefined,
        workShopCode: undefined,
        lineCode: undefined,
        sectionCode: undefined,
        pageNum: 1,
        pageSize: 10,
        matCode: null,
        matName: null,
        batchNo: null,
        storehouseCode: null,
        binCode: null,
        packing: null,
        measureUnit: null,
        stockType: this.name,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        matCode: [{ required: true, message: "物料编号不能为空", trigger: "blur" }],
        matName: [{ required: true, message: "物料名称不能为空", trigger: "blur" }],
        batchNo: [{ required: true, message: "批次号不能为空", trigger: "blur" }],
        storehouseCode: [{ required: true, message: "线边仓编号不能为空", trigger: "blur" }],
        binCode: [{ required: true, message: "库位编号不能为空", trigger: "blur" }],
        packing: [{ required: true, message: "包装规格不能为空", trigger: "blur" }],
        measureUnit: [{ required: true, message: "单位不能为空", trigger: "blur" }],
        num: [{ required: true, message: "数量不能为空", trigger: "blur" }],
      },
      formData: {},
    };
  },
  computed: {
    titleName() {
      return this.name === "mat" ? "物料" : "产品";
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询实时库存列表 */
    getList() {
      this.loading = true;
      listStock(this.queryParams).then((response) => {
        this.stockList = response.rows;
        if(this.stockList && this.stockList.length){
          this.stockList.forEach(item=>{
            item.availableNum = floatSub(item.num,item.frozenNum)
          })
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        matCode: null,
        matName: null,
        batchNo: null,
        storehouseCode: null,
        binCode: null,
        packing: null,
        measureUnit: null,
        num: null,
        remark: null,

        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleDetail(row) {
      this.formData = row;
      this.open = true;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实时库存";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getStock(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑实时库存";
      });
    },
    /** 提交按钮 */
    // submitForm() {
    //   this.$refs["form"].validate((valid) => {
    //     if (valid) {
    //       if (this.form.id != null) {
    //         updateStock(this.form).then((response) => {
    //           this.$modal.msgSuccess("编辑成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       } else {
    //         addStock(this.form).then((response) => {
    //           this.$modal.msgSuccess("新增成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       }
    //     }
    //   });
    // },
    /** 删除按钮操作 */
    // handleDelete(row) {
    //   const ids = row.id || this.ids;
    //   this.$modal
    //     .confirm('是否确认删除实时库存编号为"' + ids + '"的数据项？')
    //     .then(function () {
    //       return delStock(ids);
    //     })
    //     .then(() => {
    //       this.getList();
    //       this.$modal.msgSuccess("删除成功");
    //     })
    //     .catch(() => {});
    // },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "warehouse/stock/export",
        {
          ...this.queryParams,
        },
        `stock_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
